﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Net.Sockets;
using System.Net;
using System.Xml;
using System.Xml.Serialization;

namespace ChatServer.model
{
    
    public class Room : dbconn.DBConnector
    {
        //integer represents ID for Room in Database
        private int _roomID;
        //name of room. retrieved by using ID in Database
        private string _roomName;
        //list of UserSockets for every User that has permissions and is active. One Socket for One User in the Room
        private List<UserSocket> _activeUserSockets;
       
        //User object of Moderator for Room
        private User _moderator;

        public Room(string _RoomName)
        {
            _roomName = _RoomName;
            _activeUserSockets = new List<UserSocket>();
        }
       
        public string RoomName
        {
            get { return _roomName; }
            set { _roomName = value; }
        }

       
        public int RoomID
        {
            get { return _roomID; }
            set { _roomID = value; }
        }

        
        public List<UserSocket> ActiveUserSockets
        {
            get { return _activeUserSockets; }
            set { _activeUserSockets = value; }
        }

        public void AppendSocket(UserSocket NewUser)
        {
            
            //method socket accept as a parameter and Append it to the list
            ActiveUserSockets.Add(NewUser);
            //verify user name and ip unique
            if(ActiveUserSockets.Count != ActiveUserSockets.Distinct().Count())
            {
                //create a private method to test for broken sockets and remove from list
                RemoveUser(NewUser.IpAddress);
            }
            return;
        }

        //remove user from list after they logoff
        public void RemoveUser(string _UserIP)
        {
            foreach (UserSocket US in ActiveUserSockets)
            {
                //compare to each user socket in the list with IPaddress
                if (US.IpAddress == _UserIP)
                {
                    ActiveUserSockets.Remove(US);
                    //if ip is found in activeuserlist then we know that 
                    //no other sockets need to be removed stop searching
                    break;
                }
            }
        }
        //name of room
        //each user of the room
        //user ip
        private void UserXmlList()
        {
            
           
            //build nodes by xml
            //creating xml form and adding name of the room
            // each user of the room and user ip
            XmlWriter _xWrite = null;
            //Settings for the XML writer
            XmlWriterSettings _xSettings = new XmlWriterSettings();
            _xSettings.Indent = true;
            _xSettings.IndentChars = ("/t");

            //Create the xml of a list of users for this room
            _xWrite = XmlWriter.Create("UserList.xml", _xSettings);
            _xWrite.WriteStartDocument();
            _xWrite.WriteStartElement("Room");
            _xWrite.WriteStartElement("USERS");
             foreach (UserSocket Usr in ActiveUserSockets)
             {
                _xWrite.WriteStartElement("user");
                _xWrite.WriteElementString("uName", Usr.User.UserName);
                _xWrite.WriteElementString("userIP", Usr.IpAddress);
                _xWrite.WriteEndElement();//end "user"
                
             }
            _xWrite.WriteEndElement();//end "USERS"
            _xWrite.WriteEndElement();//end "Room"
            _xWrite.WriteEndDocument();//end "Userlist.xml"
        }

        //receives then forwards message to UserSocket
        public void ForwardMessage(string _xml)
        {
            foreach (UserSocket US in ActiveUserSockets)
                US.sendMessage(new Chat());
             
        }
       
       
    }
}
